﻿using PlatformNetCore.Common;
using PlatformNetCore.Entity;
using PlatformNetCore.Entity.Models;
using PlatformNetCore.IRepository;
using PlatformNetCore.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace PlatformNetCore.Service
{
    public class SmtpTemplateService : ISmtpTemplateService
    {
        private readonly ISmtpTemplateRepository smtpTemplateRepository;
        public SmtpTemplateService(ISmtpTemplateRepository _smtpTemplateRepository)
        {
            smtpTemplateRepository = _smtpTemplateRepository;
        }

        #region 邮件模板
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LayPager<SmtpTemplate> GetSmtpTemplatePagedList(int pageIndex, int pageSize, Expression<Func<SmtpTemplate, bool>> predicate)
        {
            var list = smtpTemplateRepository.GetPagedList(pageIndex, pageSize, predicate);
            LayPager<SmtpTemplate> pmodel = new LayPager<SmtpTemplate>();
            if (list != null)
            {
                pmodel.count = list.TotalItemCount;
                pmodel.msg = CommonConst.Success;
                pmodel.data = list.ToList();
            }
            return pmodel;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultResponse<SmtpTemplate> GetSmtpTemplate(string id)
        {
            ResultResponse<SmtpTemplate> resultResponse = new ResultResponse<SmtpTemplate>() { success = false, msg = CommonConst.Fail };
            resultResponse.data = smtpTemplateRepository.GetById(id);
            if (resultResponse.data != null)
            {
                resultResponse.success = true;
                resultResponse.msg = CommonConst.Success;
            }
            return resultResponse;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse AddSmtpTemplate(SmtpTemplate model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                response.msg = "标题不能为空!";
                return response;
            }
            model.ID = IdentityCreator.NextIdentity;
            model.AddDate = DateTime.Now;
            response.success = smtpTemplateRepository.Add(model);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse UpdateSmtpTemplate(SmtpTemplate model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                response.msg = "标题不能为空!";
                return response;
            }
            var o = smtpTemplateRepository.GetById(model.ID);
            o.Title = model.Title;
            o.Content = model.Content;
            o.SmtpCode = model.SmtpCode;
            o.UpdateBy = model.UpdateBy;
            o.UpdateDate = DateTime.Now;

            response.success = smtpTemplateRepository.Update(o);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="dataStatus"></param>
        /// <returns></returns>
        public ResultResponse UpdateSmtpTemplateState(List<string> ids, string userName, DataStatus dataStatus)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            var products = smtpTemplateRepository.GetList(s => ids.Contains(s.ID)).ToList();
            if (products != null && products.Count > 0)
            {
                switch (dataStatus)
                {
                    case DataStatus.Deleted:
                        response.success = smtpTemplateRepository.Delete(products);
                        break;
                }
                response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            }
            return response;
        }
        #endregion
    }
}
